#include "user.h"

User::User()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("C:/Codefield/CODE_QT_OWN/build-ExpressSys_v3.0/build-client/Database.db");
    if (!db.open()) {
        qDebug() << "打开失败";
    }
    else {
        qDebug() << "user打开数据库成功";
    }
}

bool User::signIn()
{
    QString logType;
    if (type == CONSUMER)   logType = "用户";
    else if (type == COURIER)    logType = "快递员";
    else    logType = "管理员";
    QString str = QString("INSERT INTO login VALUES('%1', '%2', '%3')"
                          ).arg(username).arg(password).arg(logType);
    QSqlQuery query;
    bool isSignIn = query.exec(str);

    // 在登陆表注册的同时在信息表同时注册
    str = QString("INSERT INTO userinfo VALUES('%1', null, null, 0, null, '%2')")
            .arg(username).arg(logType);
    query.exec(str);
    return isSignIn;
}

void User::setInitInfo(QString name, QString pass)
{
    username = name;
    password = pass;
}

Consumer::Consumer()
{
    type = User::CONSUMER;
}

Consumer::Consumer(QString username)
{
    this->username = username;
    type = User::COURIER;
    QSqlQuery query;
    query.prepare(QString("SELECT * FROM userinfo WHERE username = '%1'")
                  .arg(this->username));
    query.exec();
    if (query.next()) {
       name = query.value(1).toString();
       phoneNum = query.value(2).toString();
       balance = query.value(3).toDouble();
       address = query.value(4).toString();
    }
}

bool Consumer::pay(double money)
{
    if (money > balance)
        return false;
    else {
        balance -= money;
        QSqlQuery query;
        query.exec(QString("UPDATE userinfo set balance = %1 WHERE username = '%2'")
                   .arg(balance).arg(username));
        return true;
    }
}


QString User::logIn()
{
    QString logType;
    if (type == CONSUMER)   logType = "用户";
    else if (type == COURIER)    logType = "快递员";
    else    logType = "管理员";

    QSqlQuery query;
    QString str = QString("SELECT * FROM login WHERE username = '%1' and type = '%2'")
            .arg(username).arg(logType);
    query.exec(str);
    if (query.next()) {
        QString realPass = query.value(1).toString();
        return (password == realPass) ? username : "WrongID";
    }
    else
        return "loginQueryFail";
}


Admin::Admin()
{
    type = User::ADMIN;
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("Database.db");
    db.open();
}

Admin::Admin(QString username)
{
    this->username = username;
    type = User::ADMIN;
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("Database.db");
    db.open();
}

double User::charge(double money)
{
    double curMoney = 0;
    QSqlQuery query;
    QString str = QString("SELECT * FROM userinfo WHERE username = '%1'").arg(username); // 待修改,加类型参数
    query.exec(str);
    if (query.next()) {
        curMoney = query.value(3).toDouble();
    }
    curMoney += money;
    str = QString("UPDATE userinfo set balance = %1 WHERE username = '%2'").arg(curMoney).arg(username);
    query.exec(str);
    balance = curMoney;
    return curMoney;
}

Courier::Courier()
{
    type = User::COURIER;
}

Courier::Courier(QString username)
{
    type = User::COURIER;
    this->username = username;
}

void Courier::editInfo(QString name, QString phone)
{
    this->name = name;
    this->PhoneNum = phone;
    QSqlQuery query;
    if (query.exec(QString("UPDATE userinfo set name = '%1', phone = '%2' WHERE username"
                       " = '%3'").arg(name).arg(phone).arg(username)))
        qDebug() << "添加成功";
    else
        qDebug() << "添加失败";
}
